//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "DisProtProcessResult.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <iostream>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
DisProtProcessResult::DisProtProcessResult() {
  
  this->id                    = new String ();
  this->sequence              = new String ();

  this->seqPrediction         = new String ();
  this->probPrediction        = new TListE <double> ();
  
  this->percentIDPPrediction  = 0;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
DisProtProcessResult::DisProtProcessResult(const DisProtProcessResult& disProtProcessResult) {
  
  this->id                    = new String (disProtProcessResult.id);
  this->sequence              = new String (disProtProcessResult.sequence);

  this->seqPrediction         = new String (disProtProcessResult.seqPrediction);
  this->probPrediction        = new TListE <double> (disProtProcessResult.probPrediction);
  
  this->percentIDPPrediction  = disProtProcessResult.percentIDPPrediction;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
DisProtProcessResult::DisProtProcessResult(const DisProtProcessResult* disProtProcessResult) {
  
  this->id                    = new String (disProtProcessResult->id);
  this->sequence              = new String (disProtProcessResult->sequence);

  this->seqPrediction         = new String (disProtProcessResult->seqPrediction);
  this->probPrediction        = new TListE <double> (disProtProcessResult->probPrediction);
  
  this->percentIDPPrediction  = disProtProcessResult->percentIDPPrediction;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
DisProtProcessResult::~DisProtProcessResult() {
  
  if (this->id)             delete this->id;
  if (this->sequence)       delete this->sequence;
  if (this->seqPrediction)  delete this->seqPrediction;
  if (this->probPrediction) delete this->probPrediction;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void DisProtProcessResult::Id(String* id) {
  
  if (id) this->id->In(id);
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void DisProtProcessResult::Sequence(String* sequence) {
  
  if (sequence) this->sequence->In(sequence);
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void DisProtProcessResult::SeqPrediction(String* seqPrediction) {
  
  if (seqPrediction) this->seqPrediction->In(seqPrediction);
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void DisProtProcessResult::ProbPrediction(TListE <double>* probPrediction) {
  
  if (probPrediction) *(this->probPrediction) = *(probPrediction);
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void DisProtProcessResult::PercentIDPPrediction(double percentIDPPrediction) {
  
  this->percentIDPPrediction = percentIDPPrediction;
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void DisProtProcessResult::IdInput(char* text) {
  
  if (text) {
    this->id->In(text + 1);
    this->id->Trim();
  }
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void DisProtProcessResult::SequenceInput(char* text) {
  
  if (text) {
    this->sequence->In(text + 1);
    this->sequence->Trim();
  }
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void DisProtProcessResult::SeqPredictionInput(char* text) {
  
  if (text) {
    this->seqPrediction->In(text + 1);
    this->seqPrediction->Trim();
  }
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
void DisProtProcessResult::ProbPredictionInput(char* text) {
  
  this->probPrediction->Clear();
  if (text) this->SplitColumn(text + 1, this->probPrediction, ",\n");
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* DisProtProcessResult::Id(void) {
  
  return this->id;
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* DisProtProcessResult::Sequence(void) {
  
  return this->sequence;
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
String* DisProtProcessResult::SeqPrediction(void) {
  
  return this->seqPrediction;
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
TListE <double>* DisProtProcessResult::ProbPrediction(void) {
  
  return this->probPrediction;
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
double DisProtProcessResult::PercentIDPPrediction(void) {
  
  return this->percentIDPPrediction;
    
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool DisProtProcessResult::AddResidueDatas(char* line) {
  
  if (!line) return false;
  
  TListE <String>* columns = new TListE <String> ();
  
  this->SplitColumn(line, columns, " \t\n\r");
  
  if (columns->Length() != 4) { delete columns; return false; }  
  
  this->seqPrediction->Add(columns->Get(3)->Out());
  
  double* prob = new double;
  *prob = atof(columns->Get(2)->Out());
  this->probPrediction->Add(prob);
  
  if (columns) delete columns;
  
  return true;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
double DisProtProcessResult::ComputePercentIDPPrediction(void) {
  
  double percentIDP = 0;
  
  for (int i = 0; i < this->seqPrediction->Length(); i++)
    if (this->seqPrediction->Out()[i] == 'D')
      percentIDP++;
  
  percentIDP = (percentIDP * 100) / (double)this->seqPrediction->Length();
  
  return percentIDP;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool DisProtProcessResult::SplitColumn(char* buffer, TListE <String>* columns, char* characters) {

  if (!buffer || !columns || !characters) return false;

  char *part = strtok(buffer, characters);

  while (part) {
    String* cell = new String(part);
    cell->Trim();
    columns->Add(cell);
    part = strtok(NULL, characters);
  }

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool DisProtProcessResult::SplitColumn(char* buffer, TListE <String>* columns, const char* characters) {

  if (!buffer || !columns || !characters) return false;

  char *part = strtok(buffer, characters);

  while (part) {
    String* cell = new String(part);
    cell->Trim();
    columns->Add(cell);
    part = strtok(NULL, characters);
  }

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool DisProtProcessResult::SplitColumn(char* buffer, TListE <double>* columns, char* characters) {

  if (!buffer || !columns || !characters) return false;

  char *part = strtok(buffer, characters);

  while (part) {
    double* cell = new double;
    *cell = atof(part);
    columns->Add(cell);
    part = strtok(NULL, characters);
  }

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
bool DisProtProcessResult::SplitColumn(char* buffer, TListE <double>* columns, const char* characters) {

  if (!buffer || !columns || !characters) return false;

  char *part = strtok(buffer, characters);

  while (part) {
    double* cell = new double;
    *cell = atof(part);
    columns->Add(cell);
    part = strtok(NULL, characters);
  }

  return true;

}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
